<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label="$t('radio.deviceID')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-model="validateParam.radioId" v-decorator="[ 'radioId', validatorRules.radioId]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('base.name')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'name', validatorRules.name]" :placeholder="$t('base.pleaseEnter')"></a-input>
        </a-form-item>

        <a-form-item :label="$t('radio.materialCode')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['materialCode', validatorRules.materialCode]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_wradio"/>
        </a-form-item>

        <a-form-item :label="$t('radio.radioManufacturer')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  @change="handleChangeCompanyCode" v-decorator="['companyCode', validatorRules.companyCode]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_yzgs"/>
        </a-form-item>

        <a-form-item :label="$t('radio.volume')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'vol', validatorRules.vol]" :placeholder="$t('base.pleaseEnter')" style="width: 100%"/>
        </a-form-item>

        <a-form-item :label="$t('systemManagement.project')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="[ 'projectCode', validatorRules.projectCode]" :placeholder="$t('base.pleaseChoose')" @change="handleChangeProjectCode">
            <a-select-option v-for="item in projectList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label="$t('light.belongingLightPole')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-model="lightPoleCode" v-decorator="[ 'lightPoleCode', validatorRules.lightPoleCode]" :placeholder="$t('base.pleaseChoose')">
            <a-select-option v-for="item in lightPoleList" :key="item.id" :value="item.code">
              {{item.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item :label="$t('radio.belongingType')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag  v-decorator="['terminalType', validatorRules.terminalType]" :triggerChange="true" :placeholder="$t('base.pleaseChoose')"
                              dictCode="glr_ys_type"/>
        </a-form-item>

        <a-form-item :label="$t('radio.installDate')" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :placeholder="$t('base.pleaseChoose')" v-decorator="[ 'installDate',{initialValue:this.dateValue }]" :trigger-change="true" style="width: 100%"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import {duplicateCheck } from '@/api/api'
  import moment from 'moment'//导入文件

  export default {
    name: "GlrRadioModal",
    components: { 
      JDate,
    },
    data () {
      return {
        dateValue: [],
        form: this.$form.createForm(this),
        title:this.$t('base.operation'),
        width:800,
        visible: false,
        disableMixinCreated:true,
        validateParam: {
          radioId:'',
          companyCode:'',
        },
        lightPoleCode:'',
        projectList:[],
        lightPoleList:[],
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          radioId: {rules:[{required: true, message: this.$t('base.pleaseEnter')+'!'},{validator: this.validateRadioId,}]},
          name: {rules: [{required: true, message: this.$t('base.pleaseEnter')+'!'},]},
          status: {rules: [         ]},
          workStatus: {rules: [          ]},
          callStatus: {rules: [          ]},
          sid: {rules: [          ]},
          vol: {rules: [          ]},
          materialCode: {rules: [            ]},
          phoneNumber: {rules: [          ]},
          ip: {rules: [          ]},
          radioServerCode: {rules: []},
          lightPoleCode: {rules: []},
          projectCode: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!' },]},
          companyCode: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!'},{validator: this.validateRadioId,}]},
          terminalType: {rules: [{required: true, message: this.$t('base.pleaseChoose')+'!'},]},
          installDate: {rules: [          ]},
          sysCompanyCode: {rules: [          ]},
        },
        url: {
          add: "/glr/glrRadio/add",
          edit: "/glr/glrRadio/edit",
          getLightPoleList: "/glr/glrLightPole/getLightPoleList",
          getProjectList: "/glr/glrProject/getProject",
        }
      }
    },
    created () {
      this.dateValue=moment(new Date()).format("YYYY-MM-DD")
    },
    methods: {
      validateRadioId(rule, value, callback){
        const that = this;
        var params = {
          tableName: 'glr_radio',
          fieldName: 'radio_id',
          fieldVal: this.validateParam.radioId,
          dataId: this.model.id,
        };
        if (this.validateParam.companyCode){
          params.otherQueryConditions='company_code=\''+this.validateParam.companyCode+'\'';
        }
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback(that.$t('radio.alreadyExists'))
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        const that = this;
        httpAction(that.url.getProjectList,null,"post").then((res)=>{
          if(res.success){
            that.projectList=res.result;
          }else{
            //that.$message.warning(res.message);
            that.$message.warning(that.$t('base.operationFailed'));
          }
        }).finally(() => {
          that.confirmLoading = false;
        });
        that.lightPoleList=[];
        that.model={};
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'radioId','name','materialCode','status','workStatus','callStatus','sid','vol','phoneNumber','ip','radioServerCode','projectCode','lightPoleCode','companyCode','terminalType','installDate','sysCompanyCode'))
        })
        if (this.model.projectCode){
          this.handleChangeProjectCode(this.model.projectCode);
        }
        if (this.model.companyCode){
          this.validateParam.companyCode=  this.model.companyCode;
        }else {
          this.validateParam.companyCode=  "";
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                //that.$message.success(res.message);
                that.$message.success(that.$t('base.operationSuccessful'));
                that.$emit('ok');
              }else{
                //that.$message.warning(res.message);
                that.$message.warning(that.$t('base.operationFailed'));
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'radioId','name','materialCode','status','workStatus','callStatus','sid','vol','phoneNumber','ip','radioServerCode','projectCode','lightPoleCode','companyCode','terminalType','installDate','sysCompanyCode'))
      },
      handleChangeCompanyCode(val){
        this.validateParam.companyCode=val;
      },
      handleChangeProjectCode(projectCode){
        const that = this;
        that.form.setFieldsValue({'lightPoleCode': ''});
        httpAction(that.url.getLightPoleList,{projectCode:projectCode,isWisdom:'Y'},"post").then((res)=>{
          if(res.success){
            debugger;
            that.lightPoleList=res.result;
            that.lightPoleList.unshift({id: "-1",code: "",name: that.$t('base.pleaseChoose')+'!'})
          }else{
            that.$message.warning(res.message);
          }
        }).finally(() => {
          that.confirmLoading = false;
        })
      }
    }
  }
</script>